﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using MG_WIN.Util;

namespace MG_WIN.DAL
{
    /// <summary>
    /// 数据访问类:FlipChart
    /// </summary>
    public partial class FlipChart
    {
        public FlipChart()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            string strsql = "select max(fc_id)+1 from t_flip_chart";
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strsql);
            object obj = db.ExecuteScalar(dbCommand);
            if (obj != null && obj != DBNull.Value)
            {
                return int.Parse(obj.ToString());
            }
            return 1;
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int fc_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_flip_chart");
            strSql.Append(" where fc_id=" + fc_id + " ");
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(MG_WIN.Model.FlipChart model)
        {
            DbHelper db = new DbHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_flip_chart(");
            strSql.Append("fc_id,F01,fc_group,fc_order,create_datetime,update_datetime");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("" + model.fc_id + ",");
            strSql.Append("'" + model.F01 + "',");
            strSql.Append("" + model.fc_group + ",");
            strSql.Append("" + model.fc_order + ",");
            strSql.Append("'" + model.create_datetime + "',");
            strSql.Append("'" + model.update_datetime + "'");
            strSql.Append(")");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(MG_WIN.Model.FlipChart model)
        {
            DbHelper db = new DbHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_flip_chart set ");
            strSql.Append("F01='" + model.F01 + "',");
            strSql.Append("fc_group=" + model.fc_group + ",");
            strSql.Append("fc_order=" + model.fc_order + ",");
            strSql.Append("create_datetime='" + model.create_datetime + "',");
            strSql.Append("update_datetime='" + model.update_datetime + "'");
            strSql.Append(" where fc_id=" + model.fc_id + " ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int fc_id)
        {
            DbHelper db = new DbHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_flip_chart ");
            strSql.Append(" where fc_id=" + fc_id + " ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int rows = db.ExecuteNonQuery(dbCommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		/// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string fc_idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_flip_chart ");
            strSql.Append(" where fc_id in (" + fc_idlist + ")  ");
            DbHelper db = new DbHelper();
            DbCommand dbcommand = db.GetSqlStringCommand(strSql.ToString());
            int rows = db.ExecuteNonQuery(dbcommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MG_WIN.Model.FlipChart GetModel(int fc_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select   ");
            strSql.Append(" fc_id,F01,fc_group,fc_order,create_datetime,update_datetime ");
            strSql.Append(" from t_flip_chart ");
            strSql.Append(" where fc_id=" + fc_id + " ");
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            MG_WIN.Model.FlipChart model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MG_WIN.Model.FlipChart DataRowToModel(DataRow row)
        {
            MG_WIN.Model.FlipChart model = new MG_WIN.Model.FlipChart();
            if (row != null)
            {
                if (row["fc_id"] != null && row["fc_id"].ToString() != "")
                {
                    model.fc_id = int.Parse(row["fc_id"].ToString());
                }
                if (row["F01"] != null)
                {
                    model.F01 = row["F01"].ToString();
                }
                if (row["fc_group"] != null && row["fc_group"].ToString() != "")
                {
                    model.fc_group = int.Parse(row["fc_group"].ToString());
                }
                if (row["fc_order"] != null && row["fc_order"].ToString() != "")
                {
                    model.fc_order = int.Parse(row["fc_order"].ToString());
                }
                if (row["create_datetime"] != null)
                {
                    model.create_datetime = row["create_datetime"].ToString();
                }
                if (row["update_datetime"] != null)
                {
                    model.update_datetime = row["update_datetime"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select fc_id,F01,fc_group,fc_order,create_datetime,update_datetime ");
            strSql.Append(" FROM t_flip_chart ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(cmd);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" fc_id,F01,fc_group,fc_order,create_datetime,update_datetime ");
            strSql.Append(" FROM t_flip_chart ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(cmd);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_flip_chart ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            object obj = db.ExecuteScalar(dbCommand);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from t_flip_chart T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(cmd);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "t_flip_chart");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "System.Collections.Generic.List`1[Maticsoft.CodeHelper.ColumnInfo]");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<MG_WIN.Model.FlipChart> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select fc_id,F01,fc_group,fc_order,create_datetime,update_datetime ");
            strSql.Append(" FROM t_flip_chart ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<MG_WIN.Model.FlipChart> list = new List<MG_WIN.Model.FlipChart>();
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            using (IDataReader dataReader = db.ExecuteReader(cmd))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public MG_WIN.Model.FlipChart ReaderBind(IDataReader dataReader)
        {
            MG_WIN.Model.FlipChart model = new MG_WIN.Model.FlipChart();
            object ojb;
            ojb = dataReader["fc_id"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.fc_id = (int)ojb;
            }
            model.F01 = dataReader["F01"].ToString();
            ojb = dataReader["fc_group"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.fc_group = (int)ojb;
            }
            ojb = dataReader["fc_order"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.fc_order = (int)ojb;
            }
            model.create_datetime = dataReader["create_datetime"].ToString();
            model.update_datetime = dataReader["update_datetime"].ToString();
            return model;
        }

        #endregion  Method
    }
}